package com.boe.common.util;

import com.boe.common.model.ZipCompressData;
import lombok.SneakyThrows;
import lombok.var;
import net.lingala.zip4j.ZipFile;

import java.io.File;
import java.nio.charset.StandardCharsets;

public class ZipUtil {
    /**
     * 解压缩
     *
     * @param zipFile
     * @param destPath
     * @return
     */
    @SneakyThrows
    public static Boolean deCompress(File zipFile, File destPath) {
        //1.判断指定目录是否存在
        if (!destPath.exists()) {
            if (destPath.mkdirs() == false) {
                return false;
            }
        }

        var zip = new ZipFile(zipFile);
        if (zip.isValidZipFile() == false) {
            return false;
        }

        zip.setCharset(StandardCharsets.UTF_8);
        zip.extractAll(destPath.getPath());
        return true;
    }


    /**
     * 返回链式调用对象。
     *
     * @param zipFile
     * @return
     */
    public static ZipCompressData compress(File zipFile) {
        return new ZipCompressData(zipFile);
    }
}
